<?php

namespace App\Repositories\Album;

use Carbon\Carbon;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;
use Prettus\Repository\Eloquent\BaseRepository;
use Prettus\Repository\Criteria\RequestCriteria;
use App\Models\Album\AlbumGoods;
use Prettus\Repository\Traits\CacheableRepository;

/**
 * Class AlbumGoodsRepositoryEloquent.
 *
 * @package namespace App\Repositories\Album;
 */
class AlbumGoodsRepositoryEloquent extends BaseRepository implements AlbumGoodsRepository
{

    use CacheableRepository;

    /**
     * Specify Model class name
     *
     * @return string
     */
    public function model()
    {
        return AlbumGoods::class;
    }

    

    /**
     * Boot up the repository, pushing criteria
     */
    public function boot()
    {
        $this->pushCriteria(app(RequestCriteria::class));
    }

    /**
     * @author Jerry
     * @param int $id
     * @return mixed
     */
    public function findAlbumGoodsByTemplateId(int $id)
    {

        $where = [
            'ag.template_id'    =>  $id,
        ];

        $expiresAt = Carbon::now()->addHour(3);
        return Cache::remember("album:goodsTemplate{$id}", $expiresAt, function () use ($where) {
            return DB::table('album_template_goods as ag')
                ->leftJoin('goods as g', 'ag.goods_id', '=', 'g.goods_id')
                ->leftJoin('goods_storage as gs', 'ag.goods_id', '=', 'gs.goods_id')
                ->where($where)
                ->groupBy('ag.goods_id')
                ->orderBy('ag.sort', 'desc')
                ->pluck('ag.goods_id')
                ->toArray();
        });
    }
    
}
